package com.example.plumneedle.PanelView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.plumneedle.PinLu.AdapterComment1;
import com.example.plumneedle.PinLu.Comment;
import com.example.plumneedle.R;
import com.example.plumneedle.utils.TimeHelp;

import java.util.ArrayList;
import java.util.List;

import static com.mob.MobSDK.getContext;
import static com.mob.MobSDK.init;

public class HuiFu extends AppCompatActivity implements View.OnClickListener {
    static int i=0;
    private ImageView comment;
    private TextView hide_down;
    private EditText comment_content;
    private Button comment_send;
    private ConstraintLayout constraintLayout;

    // private LinearLayout rl_enroll;
    private RelativeLayout rl_comment;

    private ListView comment_list;
    private AdapterComment1 adapterComment;
    private List<Comment> data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hui_fu);
        Intent it = getIntent();
        String name=it.getStringExtra("1");
        String content=it.getStringExtra("2");
        long time=it.getLongExtra("3",0);
        String text=TimeHelp.format(time);
        TextView textView=findViewById(R.id.video_username);
        textView.setText(name);
        TextView textView1=findViewById(R.id.video_position);
        textView1.setText(content);
        TextView textView2=findViewById(R.id.video_date);
        textView2.setText(text);
        final ImageView imageView=findViewById(R.id.video_iv_good);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i%2==0) {
                    imageView.setSelected(true);
                    i++;
                }else {
                    imageView.setSelected(false);
                    i++;
                }
            }
        });
        initView();
    }
    private void initView() {

        // 初始化评论列表
        comment_list = (ListView) findViewById(R.id.comment_list1);
        // 初始化数据
        data = new ArrayList<>();
        // 初始化适配器
        adapterComment = new AdapterComment1(getContext(), data);
        // 为评论列表设置适配器
        comment_list.setAdapter(adapterComment);
        comment = (ImageView) findViewById(R.id.video_iv_comment);
        hide_down = (TextView) findViewById(R.id.hide_down1);
        comment_content = (EditText) findViewById(R.id.comment_content1);
        comment_send = (Button) findViewById(R.id.comment_send1);
      //  constraintLayout=findViewById(R.id.con);

        // rl_enroll = (LinearLayout) getActivity().findViewById(R.id.rl_enroll);
        rl_comment = (RelativeLayout) findViewById(R.id.rl_comment1);

        setListener();
    }

    /**
     * 设置监听
     */
    public void setListener(){
        comment.setOnClickListener(this);

        hide_down.setOnClickListener(this);
        comment_send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_et_comment:
            case R.id.video_iv_comment:
                // 弹出输入法
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                // 显示评论框
                // rl_enroll.setVisibility(View.GONE);
             //   constraintLayout.setVisibility(View.GONE);
                rl_comment.setVisibility(View.VISIBLE);
                break;
            case R.id.hide_down1:
                // 隐藏评论框
                //    rl_enroll.setVisibility(View.VISIBLE);
               // constraintLayout.setVisibility(View.VISIBLE);
                rl_comment.setVisibility(View.GONE);
                // 隐藏输入法，然后暂存当前输入框的内容，方便下次使用
                InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                im.hideSoftInputFromWindow(comment_content.getWindowToken(), 0);
                break;
            case R.id.comment_send1:
                sendComment();
                break;
            default:
                break;
        }
    }

    /**
     * 发送评论
     */
    public void sendComment(){
        if(comment_content.getText().toString().equals("")){
            Toast.makeText(getContext(), "评论不能为空！", Toast.LENGTH_SHORT).show();
        }else{
            // 生成评论数据
            Comment comment = new Comment();
            comment.setName("小强");
            comment.setContent(comment_content.getText().toString());
            adapterComment.addComment( comment);
            // 发送完，清空输入框
            comment_content.setText("");

            Toast.makeText(getContext(), "评论成功！", Toast.LENGTH_SHORT).show();
        }
    }
}
